﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ViewModels = FaultDiagnosisSystem.Models.ViewModels;
using System.Data.SqlClient;

namespace FaultDiagnosisSystem.DAL
{
    public class LifeLeft
    {
        /// <summary>
        /// 获取最新的剩余寿命数据
        /// </summary>
        /// <returns></returns>
        internal IList<ViewModels.LifeLeft> GetLastLifeData()
        {
            using (var context = new Models.DataModels.WindTurbineDBContext())
            {
                string msg = "SELECT t.wind_farm_id,t.turbine_id,t.component_id,t.time_start,t.life_left_estimate FROM dbo.tblVectorEstimate t "
                            + "INNER JOIN "
                            + "(SELECT wind_farm_id, turbine_id, component_id, MAX(time_start)time_s FROM dbo.tblVectorEstimate GROUP BY wind_farm_id, turbine_id, component_id) t1 "
                            + "on t.wind_farm_id = t1.wind_farm_id AND t.turbine_id = t1.turbine_id AND t.component_id = t1.component_id AND t.time_start = t1.time_s "
                            + "ORDER BY t.turbine_id,t.component_id";
                return context.Database.SqlQuery<ViewModels.LifeLeft>(msg).ToList();

            }
        }

        /// <summary>
        /// 获取指定时间点之前一年的周期剩余寿命数据，共四次，在每季度尾取数据
        /// </summary>
        /// <returns></returns>
        internal List<ViewModels.LifeLeft>[] GetCycleLifeDataArray(DateTime day)
        {
            var array = new List<ViewModels.LifeLeft>[5];

            var day1= day.AddMonths(-12).ToString("yyyy-MM-dd");
            var day2 = day.AddMonths(-9).ToString("yyyy-MM-dd");
            var day3 = day.AddMonths(-6).ToString("yyyy-MM-dd");
            var day4 = day.AddMonths(-3).ToString("yyyy-MM-dd");
            var day5 = day.ToString("yyyy-MM-dd");

            //var day1_1 = day.AddMonths(-12).ToString("yyyy-MM-dd");
            //var day1_2 = day.AddMonths(-12).AddDays(-7).ToString("yyyy-MM-dd");

            //var day2_1 = day.AddMonths(-9).ToString("yyyy-MM-dd");
            //var day2_2 = day.AddMonths(-9).AddDays(-7).ToString("yyyy-MM-dd");

            //var day3_1 = day.AddMonths(-6).ToString("yyyy-MM-dd");
            //var day3_2 = day.AddMonths(-6).AddDays(-7).ToString("yyyy-MM-dd");

            //var day4_1 = day.AddMonths(-3).ToString("yyyy-MM-dd");
            //var day4_2 = day.AddMonths(-3).AddDays(-7).ToString("yyyy-MM-dd");

            //var day5_1 = day.ToString("yyyy-MM-dd");
            //var day5_2 = day.AddDays(-7).ToString("yyyy-MM-dd");

            using (var context = new Models.DataModels.WindTurbineDBContext())
            {

                string sql = "SELECT t.wind_farm_id,t.turbine_id,t.component_id, t.time_start,MIN(t.life_left_estimate) as life_left_estimate FROM dbo.tblVectorEstimate t "
                    + "INNER JOIN "
                    + "(SELECT wind_farm_id, turbine_id, component_id, MAX(time_start)time_s FROM dbo.tblVectorEstimate  "
                    + "where time_start< @day "
                    + "GROUP BY wind_farm_id, turbine_id, component_id) t1 "
                    + "on t.wind_farm_id = t1.wind_farm_id AND t.turbine_id = t1.turbine_id AND t.component_id = t1.component_id AND t.time_start = t1.time_s "
                    + "GROUP BY t.wind_farm_id,t.turbine_id,t.component_id,t.time_start "
                    + "ORDER BY t.turbine_id,t.component_id ";

                array[0] = context.Database.SqlQuery<ViewModels.LifeLeft>(sql, new SqlParameter("@day", day1)).ToList();
                array[1] = context.Database.SqlQuery<ViewModels.LifeLeft>(sql, new SqlParameter("@day", day2)).ToList();
                array[2] = context.Database.SqlQuery<ViewModels.LifeLeft>(sql, new SqlParameter("@day", day3)).ToList();
                array[3] = context.Database.SqlQuery<ViewModels.LifeLeft>(sql, new SqlParameter("@day", day4)).ToList();
                array[4] = context.Database.SqlQuery<ViewModels.LifeLeft>(sql, new SqlParameter("@day", day5)).ToList();
                
                //string msg = "SELECT wind_farm_id,turbine_id,component_id,time_start,life_left_estimate FROM dbo.tblVectorEstimate "
                //            + "WHERE time_start< @day1_1 AND time_start> @day1_2 "
                //            + "ORDER BY turbine_id,component_id";
                //var list1= context.Database.SqlQuery<ViewModels.LifeLeft>(msg,new SqlParameter("@day1_1",day1_1), new SqlParameter("@day1_2", day1_2)).ToList();
                //array[0] = GetMinLifeList(list1, DateTime.Parse(day1_1));


                //msg="SELECT wind_farm_id,turbine_id,component_id,time_start,life_left_estimate FROM dbo.tblVectorEstimate "
                //            + "WHERE time_start< @day2_1 AND time_start> @day2_2 "
                //            + "ORDER BY turbine_id,component_id";
                //var list2 = context.Database.SqlQuery<ViewModels.LifeLeft>(msg, new SqlParameter("@day2_1", day2_1), new SqlParameter("@day2_2", day2_2)).ToList();

                //array[1] = GetMinLifeList(list2, DateTime.Parse(day2_1));

                //msg = "SELECT wind_farm_id,turbine_id,component_id,time_start,life_left_estimate FROM dbo.tblVectorEstimate "
                //            + "WHERE time_start< @day3_1 AND time_start> @day3_2 "
                //            + "ORDER BY turbine_id,component_id";
                //var list3 = context.Database.SqlQuery<ViewModels.LifeLeft>(msg, new SqlParameter("@day3_1", day3_1), new SqlParameter("@day3_2", day3_2)).ToList();

                //array[2] = GetMinLifeList(list3, DateTime.Parse(day3_1));

                //msg = "SELECT wind_farm_id,turbine_id,component_id,time_start,life_left_estimate FROM dbo.tblVectorEstimate "
                //            + "WHERE time_start< @day4_1 AND time_start> @day4_2 "
                //            + "ORDER BY turbine_id,component_id";
                //var list4 = context.Database.SqlQuery<ViewModels.LifeLeft>(msg, new SqlParameter("@day4_1", day4_1), new SqlParameter("@day4_2", day4_2)).ToList();

                //array[3] = GetMinLifeList(list4, DateTime.Parse(day4_1));

                //msg = "SELECT wind_farm_id,turbine_id,component_id,time_start,life_left_estimate FROM dbo.tblVectorEstimate "
                //            + "WHERE time_start< @day5_1 AND time_start> @day5_2 "
                //            + "ORDER BY turbine_id,component_id";
                //var list5 = context.Database.SqlQuery<ViewModels.LifeLeft>(msg, new SqlParameter("@day5_1", day5_1), new SqlParameter("@day5_2", day5_2)).ToList();

                //array[4] = GetMinLifeList(list5, DateTime.Parse(day5_1));
            }

            return array;
        }

        private void ChangeEmptyList(ref List<ViewModels.LifeLeft> oldList, DateTime day)
        {
            if (oldList.Count==0)
            {
                var list = new List<ViewModels.LifeLeft>();
                for (int i = 1; i < 29; i++)
                {
                    for (int j = 1; j < 7; j++)
                    {
                        var m = new ViewModels.LifeLeft() { wind_farm_id = "199", turbine_id = i.ToString(), component_id = j.ToString(), time_start = day, life_left_estimate = 25 };
                        list.Add(m);
                    }
                }
                oldList = list;
            }
        }

        private List<ViewModels.LifeLeft> GetMinLifeList(List<ViewModels.LifeLeft> oldList,DateTime day)
        {
            var newList = new List<ViewModels.LifeLeft>();
            var currentTurbineId = "";
            var currentComponentId = "";
            decimal life = 0;
            foreach (var item in oldList)
            {
                if (currentTurbineId != item.turbine_id)
                {
                    if (currentTurbineId != "")
                    {
                        var m = new ViewModels.LifeLeft()
                        {
                            wind_farm_id = "199",
                            turbine_id = currentTurbineId,
                            component_id = currentComponentId,
                            time_start = day,
                            life_left_estimate = life
                        };
                        newList.Add(m);
                    }
                    currentTurbineId = item.turbine_id;
                    currentComponentId = item.component_id;
                    life = item.life_left_estimate;
                }
                else if (currentComponentId != item.component_id)
                {
                    var m = new ViewModels.LifeLeft()
                    {
                        wind_farm_id = "199",
                        turbine_id = currentTurbineId,
                        component_id = currentComponentId,
                        time_start = day,
                        life_left_estimate = life
                    };
                    newList.Add(m);
                    currentComponentId = item.component_id;
                    life = item.life_left_estimate;
                }
                else if (currentComponentId == item.component_id)
                {
                    if (life > item.life_left_estimate)
                    {
                        life = item.life_left_estimate;
                    }
                }
            }

            if (currentTurbineId != "")
            {
                var n = new ViewModels.LifeLeft()
                {
                    wind_farm_id = "199",
                    turbine_id = currentTurbineId,
                    component_id = currentComponentId,
                    time_start = day,
                    life_left_estimate = life
                };
                newList.Add(n);
            }
            

            return newList;
        }
    }
}